<!DOCTYPE html>
<html>
<head>
    <title>LangChain4j Stream Demo</title>
</head>
<body>
    <input type="text" id="prompt" placeholder="输入问题...">
    <button onclick="startStream()">开始流式问答</button>
    <div id="output" style="margin-top: 20px; white-space: pre-wrap;"></div>

    <script>
        function startStream() {
            const prompt = document.getElementById('prompt').value;
            const outputDiv = document.getElementById('output');
            outputDiv.innerHTML = '';

            const eventSource = new EventSource(`http://localhost:8082/langchain/stream?prompt=${encodeURIComponent(prompt)}`);

            eventSource.onmessage = (event) => {
                outputDiv.innerHTML += event.data;
                // 自动滚动到底部
                outputDiv.scrollTop = outputDiv.scrollHeight;
            };

            eventSource.onerror = (err) => {
                console.error('Stream error:', err);
                eventSource.close();
            };
        }
    </script>
</body>
</html>